AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')


function ENT:Initialize()
	self.Entity:SetModel(  "models/props_c17/canister01a.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	self.health = 10000
	self.maxhealth = 10000
	self.maxenergy = 10
	self.maxshield = 5000
	self.maxbullets = 50
	self.maxrockets = 5
	self.energy = 0
	self.shield = 0
	self.bullets = 0
	self.rockets = 0
	self.maxturrets = 3
	self.turrets = 0
	self.heat = 0
	self.techlevel = 1
	self.capaciator = 0
	self.maxcapaciator = 1000
	self.cpu = 0
	self.maxcpu = 5
	self.user = nil
	table.insert(servertable["ship"]["shipcores"],self.Entity)
end

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "Shipcore" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:OnRemove()
	if (self.user)then
		local rp = RecipientFilter()
		if(self.user:IsValid()) then
			rp:AddPlayer(  self.user )
			umsg.Start("Hud-Shipcore", rp) 
			umsg.Long( 0 ) 
			umsg.Long( 1 ) 
			umsg.Long( 0 ) 
			umsg.Long( 1 ) 
			umsg.Long( 0 ) 
			umsg.Long( 1 ) 
			umsg.Long( 0 ) 
			umsg.Long( 0 ) 
			umsg.Long( 0 ) 
			umsg.Long( 0 ) 
			umsg.Long( 1 ) 
			umsg.Long( 0 )
			umsg.Long( 1 ) 
			umsg.Long( 0 )
			umsg.Long( 1 ) 
			umsg.Long( 0 )
			umsg.Long( 1 )
			umsg.Long( 0 )
			umsg.Long( 1 )
			umsg.End() 
		end
	end
	tableextract(servertable["ship"]["shipcores"],self.Entity)
end

function ENT:Think()
	if (self.energy < self.maxenergy) then self.energy = self.energy +1 end
	if (self.energy > self.maxenergy) then self.energy = self.maxenergy end
	if (self.shield < self.maxshield) then self.shield = self.shield +5 end
	if (self.shield > self.maxshield) then self.shield = self.maxshield end
	if (self.health < self.maxhealth) then self.health = self.health +2 end
	if (self.health > self.maxhealth) then self.health = self.health end
	if (self.capaciator < self.maxcapaciator) then self.capaciator = self.capaciator + 10 end
		if (self.bullets < self.maxbullets) then self.bullets = self.bullets + 1 end
	if self.heat > 0 then self.heat = self.heat -1 end
	if (self.heat < 0) then self.heat = 0 end
	if (self.capaciator > self.maxcapaciator) then self.capaciator = self.maxcapaciator end
	if (self.bullets > self.maxbullets) then self.bullets = self.maxbullets end
	if (self.rockets > self.maxrockets) then self.rockets = self.maxrockets end
	self:UpdateHud ()
	self.Entity:NextThink( CurTime() + 0.25)
	return true
end

function ENT:UpdateHud()
	if (self.user)then
		local rp = RecipientFilter()     // Grab a RecipientFilter object 
		if(self.user:IsValid()) then
			rp:AddPlayer(  self.user )             // Send to all players! 
			umsg.Start("Hud-Shipcore", rp) 
			umsg.Long( self.energy ) 
			umsg.Long( self.maxenergy) 
			umsg.Long( self.heat ) 
			umsg.Long( self.bullets ) 
			umsg.Long( self.maxbullets ) 
			umsg.Long( self.rockets ) 
			umsg.Long( self.maxrockets ) 
			umsg.Long( self.shield )
			umsg.Long( self.maxshield ) 
			umsg.Long( self.health )
			umsg.Long( self.maxhealth ) 
			umsg.Long( self.turrets )
			umsg.Long( self.maxturrets )
			umsg.Long( self.capaciator )
			umsg.Long( self.maxcapaciator )
			umsg.Long( self.cpu )
			umsg.Long( self.maxcpu )
			umsg.End() 
		end
	else
		self.user = player.GetByUniqueID(servertable["entitys"][self.Entity:EntIndex()]["owner"])
	end
end	

function ENT:OnTakeDamage(info)
	info:AddDamage(info:GetDamage())  --critical hit!
	Shieldhit(info,self.Entity)
end